package com.y.core.plugins.connection;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import org.beetl.sql.core.IDAutoGen;
import org.beetl.sql.core.SQLManager;

import com.y.core.config.BladeConfig;
import com.y.core.interfaces.IPlugin;
public class ConnectionPlugin implements IPlugin{
	private static Map<String, SQLManager> pool = new ConcurrentHashMap<String, SQLManager>();
	public String MASTER = "master";
	public Map<String, SQLManager> getPool(){
		return pool;
	}
	private ConnectionPlugin() { }
	private static ConnectionPlugin me = new ConnectionPlugin();
	public static ConnectionPlugin init(){
		return me;
	}
	public void start() {
		try {
			for(String key : BladeConfig.getPool().keySet()){
				SQLManager sm = BladeConfig.getPool().get(key);
				//增加自定义@AssignID注解的值, 使用方式: @Assign("uuid")
				sm.addIdAutonGen("uuid", new IDAutoGen<String>() {
					public String nextID(String arg0) {
						return UUID.randomUUID().toString();
					}
				});
				pool.put(key, sm);
			}
			if(!pool.containsKey(MASTER)){
				throw new RuntimeException("BladeConfig必须注入key值为master的sqlManager!");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public void stop() {
		pool.clear();
	}
}
